package com.tarena.lbs.business.dao.mapper;

import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StoreMapper {

    @Select({
            "<script>",
            "SELECT",
            " id AS id,",
            " business_id AS businessId,",
            " store_name AS storeName,",
            " store_phone AS storePhone,",
            " store_head_name AS storeHeadName,",
            " store_head_phone AS storeHeadPhone,",
            " province_id AS provinceId,",
            " area_id AS areaId,",
            " city_id AS cityId,",
            " store_location AS storeLocation,",
            " store_longitude AS storeLongitude,",
            " store_latitude AS storeLatitude,",
            " store_logo AS storeLogo,",
            " store_introduction AS storeIntroduction,",
            " sort AS sort,",
            " trade_time AS tradeTime,",
            " store_status AS storeStatus,",
            " create_time AS createTime,",
            " update_time AS updateTime",
            " FROM lbs_store",
            " <where>",
            "  <if test=\"q.storeName != null and q.storeName != ''\">",
            "    AND store_name LIKE CONCAT('%', #{q.storeName}, '%')",
            "  </if>",
            "  <if test=\"q.storeHeadPhone != null and q.storeHeadPhone != ''\">",
            "    AND store_head_phone = #{q.storeHeadPhone}",
            "  </if>",
            "  <if test='q.provinceId != null'> AND province_id = #{q.provinceId} </if>",
            "  <if test='q.areaId != null'> AND area_id = #{q.areaId} </if>",
            "  <if test='q.cityId != null'> AND city_id = #{q.cityId} </if>",
            "  <if test='q.businessId != null'> AND business_id = #{q.businessId} </if>",
            "  <if test='q.storeStatus != null'> AND store_status = #{q.storeStatus} </if>",
            "  <if test='q.startingTime != null'> AND create_time &gt;= #{q.startingTime} </if>",
            "  <if test='q.endTime != null'> AND create_time &lt;= #{q.endTime} </if>",
            " </where>",
            " ORDER BY id DESC",
            " LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<StoreVO> selectPage(@Param("q") StoreQuery query,
                              @Param("offset") int offset,
                              @Param("pageSize") int pageSize);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM lbs_store",
            " <where>",
            "  <if test=\"q.storeName != null and q.storeName != ''\">",
            "    AND store_name LIKE CONCAT('%', #{q.storeName}, '%')",
            "  </if>",
            "  <if test=\"q.storeHeadPhone != null and q.storeHeadPhone != ''\">",
            "    AND store_head_phone = #{q.storeHeadPhone}",
            "  </if>",
            "  <if test='q.provinceId != null'> AND province_id = #{q.provinceId} </if>",
            "  <if test='q.areaId != null'> AND area_id = #{q.areaId} </if>",
            "  <if test='q.cityId != null'> AND city_id = #{q.cityId} </if>",
            "  <if test='q.businessId != null'> AND business_id = #{q.businessId} </if>",
            "  <if test='q.storeStatus != null'> AND store_status = #{q.storeStatus} </if>",
            "  <if test='q.startingTime != null'> AND create_time &gt;= #{q.startingTime} </if>",
            "  <if test='q.endTime != null'> AND create_time &lt;= #{q.endTime} </if>",
            " </where>",
            "</script>"
    })
    Long count(@Param("q") StoreQuery query);

    @Select("SELECT id AS id, business_id AS businessId, store_name AS storeName, store_phone AS storePhone, store_head_name AS storeHeadName, store_head_phone AS storeHeadPhone, province_id AS provinceId, area_id AS areaId, city_id AS cityId, store_location AS storeLocation, store_longitude AS storeLongitude, store_latitude AS storeLatitude, store_logo AS storeLogo, store_introduction AS storeIntroduction, sort AS sort, trade_time AS tradeTime, store_status AS storeStatus, create_time AS createTime, update_time AS updateTime FROM lbs_store WHERE id = #{id}")
    StoreVO selectDetail(@Param("id") Integer id);

    @Insert({
            "INSERT INTO lbs_store(",
            " business_id, store_name, store_phone, store_head_name, store_head_phone,",
            " store_location, store_longitude, store_latitude, store_logo, store_introduction,",
            " sort, trade_time, store_status, create_time, update_time, province_id, city_id, area_id",
            ") VALUES(",
            " #{businessId}, #{storeName}, #{storePhone}, #{storeHeadName}, #{storeHeadPhone},",
            " #{storeLocation}, #{storeLongitude}, #{storeLatitude}, #{storeLogo}, #{storeIntroduction},",
            " #{sort}, #{tradeTime}, #{storeStatus}, #{createTime}, #{updateTime}, #{provinceId}, #{cityId}, #{areaId}",
            ")"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(StorePO po);

    @Update({
            "<script>",
            "UPDATE lbs_store",
            " <set>",
            "  <if test='businessId != null'> business_id = #{businessId}, </if>",
            "  <if test='storeName != null'> store_name = #{storeName}, </if>",
            "  <if test='storePhone != null'> store_phone = #{storePhone}, </if>",
            "  <if test='storeHeadName != null'> store_head_name = #{storeHeadName}, </if>",
            "  <if test='storeHeadPhone != null'> store_head_phone = #{storeHeadPhone}, </if>",
            "  <if test='storeLocation != null'> store_location = #{storeLocation}, </if>",
            "  <if test='storeLongitude != null'> store_longitude = #{storeLongitude}, </if>",
            "  <if test='storeLatitude != null'> store_latitude = #{storeLatitude}, </if>",
            "  <if test='storeLogo != null'> store_logo = #{storeLogo}, </if>",
            "  <if test='storeIntroduction != null'> store_introduction = #{storeIntroduction}, </if>",
            "  <if test='sort != null'> sort = #{sort}, </if>",
            "  <if test='tradeTime != null'> trade_time = #{tradeTime}, </if>",
            "  <if test='storeStatus != null'> store_status = #{storeStatus}, </if>",
            "  <if test='createTime != null'> create_time = #{createTime}, </if>",
            "  <if test='updateTime != null'> update_time = #{updateTime}, </if>",
            "  <if test='provinceId != null'> province_id = #{provinceId}, </if>",
            "  <if test='cityId != null'> city_id = #{cityId}, </if>",
            "  <if test='areaId != null'> area_id = #{areaId} </if>",
            " </set>",
            " WHERE id = #{id}",
            "</script>"
    })
    int update(StorePO po);

    @Delete("DELETE FROM lbs_store WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);

    @Select("SELECT id AS id, business_id AS businessId, store_name AS storeName, store_phone AS storePhone, store_head_name AS storeHeadName, store_head_phone AS storeHeadPhone, province_id AS provinceId, area_id AS areaId, city_id AS cityId, store_location AS storeLocation, store_longitude AS storeLongitude, store_latitude AS storeLatitude, store_logo AS storeLogo, store_introduction AS storeIntroduction, sort AS sort, trade_time AS tradeTime, store_status AS storeStatus, create_time AS createTime, update_time AS updateTime FROM lbs_store WHERE business_id = #{businessId}")
    List<StoreVO> selectByBusinessId(@Param("businessId") Integer businessId);
}
